﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dropthings.Util;
using Dropthings.Business.Facade;
using Dropthings.Web.Util;
using System.Data;

public partial class Admin_AdminUserOrganizeIdentify : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            ControlDataBind();
    }

    private void ControlDataBind()
    {
        var facade = Services.Get<Facade>();
        {
            lvItems.DataSource = facade.GetPagedMember();
            lvItems.DataBind();
            Multiview.ActiveViewIndex = 0;
        }
    }

    protected void CancelClicked(object sender, EventArgs e)
    {
        ControlDataBind();
    }


    protected void ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        if (string.Equals(e.CommandName, "EditUserOrganize", StringComparison.OrdinalIgnoreCase))
        {
            var facade = Services.Get<Facade>();
            {
                cblUserOrganize.DataSource = facade.GetOrganizeBaseInfoNoHandle();
                cblUserOrganize.DataTextField = "OrganizeName";
                cblUserOrganize.DataValueField = "OrganizeIdentify";
                cblUserOrganize.DataBind();
                cblUserOrganize.Items.Add(new ListItem("全部建筑", "000"));
                hfUserName.Value = e.CommandArgument.ToString();
                string organize = facade.GetUserOrganizeIdentify(new Guid(hfUserName.Value));
                if (!string.IsNullOrEmpty(organize))
                {
                    foreach (ListItem li in cblUserOrganize.Items)
                    {
                        if (string.Equals(li.Value, organize, StringComparison.OrdinalIgnoreCase))
                        {
                            li.Selected = true;
                            hfUserNameIsExist.Value = "1";
                            break;
                        }
                    }
                }
                Multiview.ActiveViewIndex = 1;
            }
        }
    }

    protected void SaveClicked(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            var facade = Services.Get<Facade>();
            {
                try
                {
                    if (hfUserNameIsExist.Value == "1")
                        facade.UpdateUserOrganizeIdentify(new Guid(hfUserName.Value), cblUserOrganize.SelectedValue);
                    else
                        facade.AddUserOrganizeIdentify(new Guid(hfUserName.Value), cblUserOrganize.SelectedValue);
                    ControlDataBind();
                    WebUtil.ShowMessage(lblMessage, "编辑成功", false);
                }
                catch (ArgumentException ex)
                {
                    WebUtil.ShowMessage(lblMessage, ex.Message, false);
                }
            }
        }
    }

    
}